<template>
    <view>
        <!-- <view class="time-picker_content">
			<view class="{{['time-picker_content-time',activeTime == 0 ? 'active':'',timeData[0] ? 'default':'']}}"
				bindtap="pickerTimeChange(0)">
				{{timeData[0] || '开始时间'}}
			</view>
			<view class="time-picker_content-center">
				至
			</view>
			<view class="{{['time-picker_content-time',activeTime == 1 ? 'active':'',timeData[1] ? 'default':'']}}"
				bindtap="pickerTimeChange(1)">
				{{timeData[1] || '结束时间'}}
			</view>
		</view>
		<picker-view indicator-style="{{indicatorStyle}}" value="{{value}}" bindchange="bindChange" class="picker-view">
			<picker-view-column>
				<view class="item" wx:for="{{years}}" wx:key="index">{{item + end_year + 1}}年</view>
			</picker-view-column>
			<picker-view-column>
				<view class="item" wx:for="{{months}}" wx:key="index">{{item + 1}}月</view>
			</picker-view-column>
			<picker-view-column>
				<view class="item" wx:for="{{days}}"  wx:key="index">{{item + 1}}日</view>
			</picker-view-column>
		</picker-view>
		<view class="picker-button" bindtap="pickerTimeSubmit">
			完成
		</view> -->
    </view>
</template>

<script>
export default {
    data() {
        return {
            months: '',
            days: '',
            value: ''
        };
    },
    created(options) {
        // if (options.time != "null") {
        //     this.timeData = options.time.split("/");
        // }
        this.datas()
        this.getTime();
    },
    methods: {
        datas() {
            let currentTime = new Date();
            let end_year = 2000;
            let currentYear = currentTime.getFullYear();
            let currentMonth = currentTime.getMonth() + 1;
            let currentDay = currentTime.getDate();
            let years = parseInt(currentTime.getFullYear() - end_year);
            let months = parseInt(currentTime.getMonth() + 1);
            let days = parseInt(currentTime.getDate());
            return {
                activeTime: 0,
                currentYear,
                currentMonth,
                currentDay,
                end_year,
                years,
                months,
                days,
                timeData: ['', ''],
                value: '',
                indicatorStyle: `height: 50px;`
            };
        },

        pickerTimeChange(val) {
            let _data = this.timeData[val].split('-').map((i) => parseInt(i));

            this.activeTime = val;

            if (this.currentYear == _data[0]) {
                this.months = this.currentMonth;
            } else {
                this.months = 12;
            }

            if (this.currentYear == _data[0] && this.currentMonth == _data[1]) {
                this.days = this.currentDay;
            } else {
                this.days = this.mGetDate(_data[0], _data[1]);
            }

            _data[1] = --_data[1];
            _data[2] = --_data[2];
            this.value = _data;
            debugger;
        },

        // 获取当前时间
        getTime() {
            let currentTime = new Date();
            let year = currentTime.getFullYear();
            let month = currentTime.getMonth() + 1;
            let day = currentTime.getDate();

            if (!this.timeData[0]) {
                this.setData({
                    'timeData[0]': `${year}-${month}-${day}`
                });
            }

            if (!this.timeData[1]) {
                this.setData({
                    'timeData[1]': `${year}-${month}-${day}`
                });
            } // debugger
            // this.days = this.mGetDate(year, month);

            this.years = parseInt(year - this.end_year);

            if (this.timeData[0]) {
                let _data = this.timeData[0].split('-');

                _data[1] = --_data[1];
                _data[2] = --_data[2];

                if (this.currentYear == _data[0]) {
                    // this.months = this.currentMonth;
                    this.setData({
                        months: this.currentMonth
                    });
                } else {
                    // this.months = 12;
                    this.setData({
                        months: 12
                    });
                }

                if (this.currentYear == _data[0] && this.currentMonth == _data[1] + 1) {
                    // this.days = this.currentDay;
                    this.setData({
                        days: this.currentDay
                    });
                } else {
                    // this.days = this.mGetDate(year, month);
                    this.setData({
                        days: this.mGetDate(year, month)
                    });
                }

                _data[0] = _data[0] - this.end_year - 1; // this.value = _data;

                this.setData({
                    value: _data
                }); // console.log('years',this.data.years);

                console.log('this', this);
            } else {
                // this.value = [9999, month - 1, day - 1]
                this.setData({
                    value: [9999, month - 1, day - 1]
                });
            }
        },

        // 获取本月天数
        mGetDate(year, month) {
            let days = new Date(year, month, 0).getDate();
            return days;
        },

        bindChange(e) {
            const val = e.detail.value;
            let year = val[0] + this.end_year + 1;
            let month = val[1] + 1;
            let day = val[2] + 1;
            let index = this.activeTime; // currentYear

            if (this.currentYear == year) {
                this.months = this.currentMonth;
            } else {
                this.months = 12;
            }

            if (this.currentYear == year && this.currentMonth == month) {
                this.days = this.currentDay;
            } else {
                this.days = this.mGetDate(year, month);
            }

            this.$set(this.timeData, index, `${year}-${month}-${day}`);
        },

        // 确定选择时间
        pickerTimeSubmit() {
            if (!this.timeData[1] || !this.timeData[0]) {
                this.$AppUtils.errMsg('请选择时间范围');
                return;
            }

            let pages = getCurrentPages(); //当前页

            let beforePage = pages[pages.length - 2]; //上个页面

            beforePage.$vm.check_time = this.timeData.join('/');
            beforePage.$vm.period = 0;
            uni.navigateBack();
            beforePage.$vm.getServerData();
        }
    }
};
</script>
<style>
@import './checkTime.css';
</style>
